﻿using System.IO;
using Nini.Ini;

namespace WizAppHelper
{
    class Setting
    {
        #region 变量声明区
        private const string SettingFileName = "Setting.ini";

        private const string RecentPathSectionName = "RecentPath";

        private const string ComboBoxItemsSectionName = "ComboBoxItems";

        private static Setting _singleton;

        private readonly IniDocument _settingDocument;
        #endregion

        private Setting()
        {
            if (File.Exists(SettingFileName))
                _settingDocument = new IniDocument(SettingFileName);
            else
                _settingDocument = new IniDocument();
        }

        internal static Setting GetInstance()
        {
            if (_singleton == null)
                _singleton = new Setting();

            return _singleton;
        }

        internal void Save()
        {
            _settingDocument.Save(SettingFileName);
        }

        #region 要保存的字段
        /// <summary>
        /// 最后打开的有效的Wiz安装目录
        /// </summary>
        public string RecentPublicPath
        {
            get
            {
                if (_settingDocument.Sections.Contain(RecentPathSectionName))
                    return _settingDocument.Sections[RecentPathSectionName].GetValue("Public");

                return null;
            }
            set
            {
                if (!_settingDocument.Sections.Contain(RecentPathSectionName))
                {
                    _settingDocument.Sections.Add(new IniSection(RecentPathSectionName));
                }

                _settingDocument.Sections[RecentPathSectionName].Set("Public", value);
            }
        }
        /// <summary>
        /// 最后打开的有效的个人知识库目录
        /// </summary>
        public string RecentPersonalPath
        {
            get
            {
                if (_settingDocument.Sections.Contain(RecentPathSectionName))
                    return _settingDocument.Sections[RecentPathSectionName].GetValue("Personal");

                return null;
            }
            set
            {
                if (!_settingDocument.Sections.Contain(RecentPathSectionName))
                {
                    _settingDocument.Sections.Add(new IniSection(RecentPathSectionName));
                }

                _settingDocument.Sections[RecentPathSectionName].Set("Personal", value);
            }
        }
        /// <summary>
        /// plugin.ini的[Common]节点下的AppType可选值
        /// </summary>
        public string[] AppType
        {
            get
            {
                if (!_settingDocument.Sections.Contain(ComboBoxItemsSectionName))
                    return null;

                var items = _settingDocument.Sections[ComboBoxItemsSectionName].GetValue("AppType");
                if (items == null)
                    return null;

                return items.Split('|');
            }
            set
            {
                if (!_settingDocument.Sections.Contain(ComboBoxItemsSectionName))
                {
                    _settingDocument.Sections.Add(new IniSection(ComboBoxItemsSectionName));
                }

                _settingDocument.Sections[ComboBoxItemsSectionName].Set("AppType", string.Join("|", value));
            }
        }
        /// <summary>
        /// plugin.ini的[Plugin_N]节点下的MenuType可选值
        /// </summary>
        public string[] MenuType
        {
            get
            {
                if (!_settingDocument.Sections.Contain(ComboBoxItemsSectionName))
                    return null;

                var items = _settingDocument.Sections[ComboBoxItemsSectionName].GetValue("MenuType");
                if (items == null)
                    return null;

                return items.Split('|');
            }
            set
            {
                if (!_settingDocument.Sections.Contain(ComboBoxItemsSectionName))
                {
                    _settingDocument.Sections.Add(new IniSection(ComboBoxItemsSectionName));
                }

                _settingDocument.Sections[ComboBoxItemsSectionName].Set("MenuType", string.Join("|", value));
            }
        }
        /// <summary>
        /// plugin.ini的[Plugin_N]节点下的Type可选值
        /// </summary>
        public string[] Type
        {
            get
            {
                if (!_settingDocument.Sections.Contain(ComboBoxItemsSectionName))
                    return null;

                var items = _settingDocument.Sections[ComboBoxItemsSectionName].GetValue("Type");
                if (items == null)
                    return null;

                return items.Split('|');
            }
            set
            {
                if (!_settingDocument.Sections.Contain(ComboBoxItemsSectionName))
                {
                    _settingDocument.Sections.Add(new IniSection(ComboBoxItemsSectionName));
                }

                _settingDocument.Sections[ComboBoxItemsSectionName].Set("Type", string.Join("|", value));
            }
        }
        #endregion
    }
}
